﻿using Microsoft.Xna.Framework;
using System;
using Terraria;
using Terraria.Audio;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Content.Projectiles.Ammo
{
    public class GraniteEnergySlingBulletSurge : ModProjectile
    {
        public override void SetStaticDefaults()
        {
            ProjectileID.Sets.CultistIsResistantTo[Projectile.type] = true;
        }

        public override void SetDefaults()
        {
            Projectile.width = 22;
            Projectile.height = 22;
            Projectile.friendly = true;
            Projectile.DamageType = DamageClass.Throwing;
            Projectile.extraUpdates = 1;
            Projectile.timeLeft = 300;
            Projectile.penetrate = -1;
            Projectile.ignoreWater = true;
            Projectile.tileCollide = false;
        }

        public override Color? GetAlpha(Color lightColor)
        {
            return Color.White;
        }
        public int dir = 1;


        public override void AI()
        {
            Projectile.ai[0]++;

            if (Projectile.ai[0] < 30f)
            {
                Projectile.friendly = false;
            }
            else
            {
                Projectile.friendly = true;
            }

            if (Projectile.ai[0] == 0)
            {

                if (Projectile.alpha == 255)
                {
                    if (Main.rand.Next(2) == 0)
                    {
                        dir = -1;
                    }
                    else
                    {
                        dir = 1;
                    }
                }
            }

            Projectile.rotation += 0.1f * dir;

            if (Math.Abs(Projectile.velocity.X) > 3 || Math.Abs(Projectile.velocity.Y) > 3)
            {
                Projectile.velocity *= 0.9f;
            }

            Lighting.AddLight(Projectile.position, 0.6f, 0.75f, 1f);
            int num97 = Dust.NewDust(new Vector2(Projectile.position.X, Projectile.position.Y), Projectile.width, Projectile.height, 15, 0f, 0f, 100, default, 1f);
            Main.dust[num97].noGravity = true;

            if (Projectile.owner == Main.myPlayer && Projectile.timeLeft <= 3)
            {
                Projectile.tileCollide = false;
                Projectile.ai[1] = 0f;
                Projectile.alpha = 255;

                Projectile.position.X = Projectile.position.X + Projectile.width / 2;
                Projectile.position.Y = Projectile.position.Y + Projectile.height / 2;
                Projectile.width = 80;
                Projectile.height = 80;
                Projectile.position.X = Projectile.position.X - Projectile.width / 2;
                Projectile.position.Y = Projectile.position.Y - Projectile.height / 2;
            }

            if (Projectile.ai[0] > 10f)
            {
                if (Projectile.localAI[0] == 0f)
                {
                    AdjustMagnitude(ref Projectile.velocity);
                    Projectile.localAI[0] = 1f;
                }
                Vector2 move = Vector2.Zero;
                float distance = 600f;
                bool target = false;
                for (int k = 0; k < 200; k++)
                {
                    if (Main.npc[k].CanBeChasedBy(this, false))
                    {
                        Vector2 newMove = Main.npc[k].Center - Projectile.Center;
                        float distanceTo = (float)Math.Sqrt(newMove.X * newMove.X + newMove.Y * newMove.Y);
                        if (distanceTo < distance)
                        {
                            move = newMove;
                            distance = distanceTo;
                            target = true;
                        }
                    }
                }
                if (target)
                {
                    float moveMult = (float)(distance * 0.041666667); 
                    if (moveMult < 2)
                    {
                        moveMult = 2; 
                    }
                    AdjustMagnitude(ref move);
                    Projectile.velocity = moveMult * Projectile.velocity + move; 
                    AdjustMagnitude(ref Projectile.velocity);
                }
            }
        }

        private void AdjustMagnitude(ref Vector2 vector)
        {
            float magnitude = (float)Math.Sqrt(vector.X * vector.X + vector.Y * vector.Y);
            if (magnitude > 15f)
            {
                vector *= 15f / magnitude;
            }
        }
        public bool canReduceTime = true;

        public override bool OnTileCollide(Vector2 oldVelocity) 
        {
            if (canReduceTime)
            {
                canReduceTime = false;
                Projectile.timeLeft = 2;
            }
            Projectile.velocity *= 0f;
            return false;
        }

        public override void OnHitNPC(NPC target, NPC.HitInfo hit, int damageDone)
        {
            if (canReduceTime)
            {
                canReduceTime = false;
                Projectile.timeLeft = 2;
                Projectile.velocity *= 0f;
            }
            Projectile.usesLocalNPCImmunity = true;
            Projectile.localNPCImmunity[target.whoAmI] = 10;
            target.immune[Projectile.owner] = 0;           
        }

        public override void OnHitPlayer(Player target, Player.HurtInfo info)
        {
            if (canReduceTime)
            {
                canReduceTime = false;
                Projectile.timeLeft = 2;
                Projectile.velocity *= 0f;
            }
        }

        public override void OnKill(int timeLeft)
        {
            SoundEngine.PlaySound(SoundID.Item14, Projectile.position);
            Projectile.position.X = Projectile.position.X + Projectile.width / 2;
            Projectile.position.Y = Projectile.position.Y + Projectile.height / 2;
            Projectile.width = 22;
            Projectile.height = 22;
            Projectile.position.X = Projectile.position.X - Projectile.width / 2;
            Projectile.position.Y = Projectile.position.Y - Projectile.height / 2;
            for (int i = 0; i < 40; i++)
            {
                int num767 = Dust.NewDust(new Vector2(Projectile.position.X, Projectile.position.Y), Projectile.width, Projectile.height, 15, 0f, 0f, 100, default, 1f);
                Main.dust[num767].noGravity = true;
                Dust dust = Main.dust[num767];
                dust.velocity *= 3.5f;
            }
        }
    }
}
